#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include <QHash>
#include <QGraphicsScene>

class QTcpSocket;
class QTcpServer;
namespace Ui {
    class MainWindow;
}

namespace Opl { namespace Gui{
        class Window;
    };};

class ProgramExecute;
class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    MainWindow(QWidget *parent = 0);
    ~MainWindow();

public slots:
    void sendGeometryChangeEvent(quint32 id, const QRectF &rect);
    void sendMousePressEvent(quint32 id, const QPointF &point);
    void sendMouseReleaseEvent(quint32 id, const QPointF &point);
    void sendMouseMoveEvent(quint32 id, const QPointF &point);
    void sendKeyPressEvent(quint32 id, quint32 key, ushort unicode, uint modifiers);
    void sendKeyReleaseEvent(quint32 id, quint32 key, ushort unicode, uint modifiers);

protected slots:
    void handleRequest();
    void handleConnection();
    void handleConnectionError();
    void removeConnection();

    void executeScript();

    // requests
    quint32 createWindow(quint32 parentId);
    void destroyWindow(quint32 id);
    void showWindow(quint32 id);
    void hideWindow(quint32 id);
    void raiseWindow(quint32 id);
    void lowerWindow(quint32 id);
    void updateWindow(quint32 id, const QRectF &rect);
    void setWindowGeometry(quint32 id, const QRectF &rect);
private:
    void connectWindow(Opl::Gui::Window* wnd);
    Ui::MainWindow *ui;
    QTcpServer *m_server;
    QHash<quint32, QTcpSocket *> m_connections;
    // resources
    QHash<quint32, Opl::Gui::Window*> m_windows;
    // scenegraph
    QGraphicsScene m_scene;

    ProgramExecute* executor;
};

#endif // MAINWINDOW_H
